package dao

import (
	"fmt"
	"frontend-server/db"
	model "frontend-server/model/gv_im"
	"time"
)

type UserResult struct {
	ID       uint   `json:"id"`
	Nickname string `json:"nickname"`
	Email    string `json:"email"`
	Phone    int    `json:"phone"`
	Sex      int    `json:"sex"`
	Avatar   string `json:"avatar"`
	IsFriend bool   `json:"is_friend"` // 是否为好友 true 是 false 否
}
type UserIDSResult struct {
	IDS []uint `json:"ids"`
}
type UserInfoResult struct {
	ID       uint      `json:"id"`
	Nickname string    `json:"nickname"`
	Username string    `json:"username"`
	Fullname string    `json:"fullname"`
	Birthday time.Time `json:"birthday"`
	Email    string    `json:"email"`
	Phone    int       `json:"phone"`
	Sex      int       `json:"sex"`
	Avatar   string    `json:"avatar"`
}

// 创建用户
func CreateGvImUser(user model.GvImUser) string {
	db := db.GetImDB()
	err := db.Table("gv_im_user").Create(&user).Error.Error()
	return err
}

// 查询用户名和 id
func GetGvImUserByUsernameOrId(id uint, username string) (model.GvImUser, error) {
	db := db.GetImDB()
	user := model.GvImUser{}
	err := db.Table("gv_im_user").Where("id = ? AND username = ?", id, username).First(&user).Error
	return user, err
}

// 根据用户id列表查询
func GetGvImUserByIDS(ids []uint) ([]model.GvImUser, error) {
	db := db.GetImDB()
	users := []model.GvImUser{}
	err := db.Table("gv_im_user").Find(&users, ids).Error
	if err != nil {
		return nil, err
	}
	fmt.Println(users)
	return users, err
}

// 查询用户邮箱或手机号
func GetGvImUserByEmailOrPhone(email, phone string) (model.GvImUser, error) {
	db := db.GetImDB()
	user := model.GvImUser{}
	err := db.Table("gv_im_user").Where("email = ? OR phone = ?", email, phone).First(&user).Error
	return user, err
}

// 根据用户id修改昵称
func ModifyGvImUserByNickname(id uint, nickname string) error {
	db := db.GetImDB()
	err := db.Table("gv_im_user").Where("id = ?", id).Update("nickname", nickname).Error
	return err
}

// 根据用户id修改性别
func ModifyGvImUserBySex(id uint, sex int) error {
	db := db.GetImDB()
	err := db.Table("gv_im_user").Where("id = ?", id).Update("sex", sex).Error
	return err
}

// 根据用户id修改生日
func ModifyGvImUserByBirthday(id uint, birthday string) error {
	db := db.GetImDB()
	err := db.Table("gv_im_user").Where("id = ?", id).Update("birthday", birthday).Error
	return err
}

// 根据用户 id 修改密码
func ModifyGvImUserByPassword(id uint, password string) string {
	db := db.GetImDB()
	err := db.Table("gv_im_user").Where("id = ?", id).Update("password", password).Error.Error()
	return err
}

// 根据用户id列表查询
func GetGvImUserByUserRooms(userRooms []model.GvImUserRoom) ([]UserResult, error) {
	db := db.GetImDB()
	users := []UserResult{}
	var err error
	for _, ur := range userRooms {
		user := model.GvImUser{}
		err = db.Table("gv_im_user").Where("id = ?", ur.UserID).First(&user).Error
		if err != nil {
			return nil, err
		}
		users = append(users, UserResult{
			ID:       user.ID,
			Nickname: user.Nickname,
			Email:    user.Email,
			Phone:    user.Phone,
			Sex:      user.Sex,
			Avatar:   user.Avatar,
			IsFriend: true,
		})
	}
	return users, nil
}
